<template>
  <el-dialog
    title="门禁授权"
    :visible.sync="dialogVisible"
    width="550px"
    class="popup height50"
    :before-close="handleClose">
    <div class="scrollbar-wrap">
      <el-scrollbar class="scrollbar-x" style="height:100%">
        <el-form :model="formInline" :rules="rules" ref="formInline" label-width="100px">
          <el-form-item label="授权模式:">
            <el-radio-group v-model="modeRole">
              <el-radio :label="1">按空间结构授权</el-radio>
              <el-radio :label="2">按个人授权</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="空间结构:" v-if="modeRole == 1">
            <tree-select ref="treeSelect"
              :data="officeTreeList"
              :el="'formInline.eptIdList'"
              placeholder="请选择"
              @parent-event="popoverHide"
              multiple
              clearable
              @popoverHide="popoverHide">
            </tree-select>
          </el-form-item>
          <el-form-item label="个人授权:" v-if="modeRole == 2">
            <el-select v-model="formInline.username" filterable placeholder="请选择">
              <el-option
                v-for="item in this.$config.selectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备模式:">
            <el-radio-group v-model="modeDevice">
              <el-radio :label="1">设备组</el-radio>
              <el-radio :label="2">单个设备</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="设备组:" v-if="modeDevice == 1">
            <el-select v-model="formInline.username" filterable placeholder="请选择">
              <el-option
                v-for="item in this.$config.selectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单个设备:" v-if="modeDevice == 2">
            <tree-select ref="treeSelect"
              :data="officeTreeList"
              :el="'formInline.eptIdList'"
              placeholder="请选择"
              multiple
              clearable
              @parent-event="popoverHide"
              @popoverHide="popoverHide">
            </tree-select>
          </el-form-item>
          <el-form-item label="已授权设备:" prop="startTime">
            <el-tree
              :data="officeTreeList"
              :style="{marginTop: '7px'}"
              show-checkbox
              node-key="id"
              :default-expanded-keys="['5']"
              :default-checked-keys="[5]">
            </el-tree>
          </el-form-item>
          <el-form-item label="开始时间:" prop="startTime">
            <el-date-picker
              v-model="formInline.startTime"
              :picker-options="startTime"
              type="datetime"
              placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间:" prop="endTime">
            <el-date-picker
              v-model="formInline.endTime"
              :picker-options="endTime"
              type="datetime"
              placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="备注:">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="formInline.username">
            </el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
    </div>
    
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" size="small"  @click="handleAdd">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import TreeSelect from '@/components/TreeSelect'
import { addPersonnel } from '@/api/personnel'
export default {
  components: {
    TreeSelect
  },
  data() {
    return {
      dialogVisible: false,
      modeRole: 1, // 授权模式
      modeDevice: 1, // 设备模式
      formInline: {
        username: '',
        password: '',
        phone: '',
        role: ['1']
      },
      defaultProps: {
        parent: 'parentId',   // 父级唯一标识
        value: 'id',          // 唯一标识
        label: 'label',       // 标签显示
        children: 'children', // 子级
      },
      
      officeTreeList: this.$config.treeList,//树形结构数据

      // 开始时间
      startTime: {
        disabledDate: time => {
          if (this.formInline.endTime) {
            return (
              time.getTime() > new Date(this.formInline.endTime).getTime()
            );
          } else {
            return time.getTime() > Date.now();
          }
        }
      },
      // 结束时间
      endTime: {
        disabledDate: time => {
          if (this.formInline.startTime) {
            return (
              time.getTime() > Date.now() ||
              time.getTime() < new Date(this.formInline.startTime).getTime()
            );
          } else {
            return time.getTime() > Date.now();
          }
        }
      },
      
    };
  },
  computed: {
    rules() {
      return {
        // pass: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ],
        // checkPass: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ],
        // age: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ]
      }
    }
      
  },
  methods: {
    togglePopup(flag) {
      this.dialogVisible = flag
    },
    handleClose(done) {
     this.togglePopup(false)
    },
    popoverHide (checkedIds, checkedData, el) {
      console.log(checkedIds, checkedData)
      if (el) {
        eval( ' ( this.' + el +' = checkedIds) ' )
        console.log(2,this.formInline.eptIdList)
      }
    },
    handleAdd() {
      addPersonnel(this.formInline)
        .then(res => {
          if (res.code === 0) {
            this.$message.success(res.msg || '成功')
            this.togglePopup(false)
            this.$emit('refresh')
          }
        })
        .catch(err => {
          this.togglePopup(false)
        })
    }
  }
};
</script>

<style lang="scss" scoped>
@import '~styles/popup.scss'
</style>